#ifndef JOINTSTATUSWIDGET_H
#define JOINTSTATUSWIDGET_H

#include <QWidget>
#include <QJsonObject>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QMap>
#include <QLineEdit>


#include "DdsClientWrapper.hpp"
#include <QHash>
#include <QJsonParseError>
#include <QJsonValue>
#include "cnp.hpp"
#include <vector>
#include <QJsonArray>
#include <mutex>
#include<QMutex>


class DdsClientWrapper;      // dds封装类
struct mMessage {
    QString device_id;
    QString timestamp;
    QHash<QString, std::vector<double>> mContent;
};

extern mMessage mStatus;
struct mEventmsg {
    QString device_id;
    QString timestamp;
    QHash<QString, QString> mContent;
};


extern QHash<QString, mEventmsg> mEvent;

class JointStatusWidget : public QWidget {
    Q_OBJECT

public:
    explicit JointStatusWidget(const QJsonObject& status, QWidget* parent = nullptr);
    void updateJointPosition(const QString& jointName, const QString& newPosition);
    void updateJointVelocity(const QString& jointName, const QString& newVelocity);


    void createAndSubscribeNewNode(uint32_t domainId, const QString& deviceID);
    //全局变量
signals:
    void tcpMessageReceived();
    void upSubEvent(const QStringList &evt);


private:

    DdsClientWrapper *ddsClient_;

    void createJointControl(const QString& jointName, const QString& positionValue, const QString& velocityValue, QVBoxLayout* layout);
    QMap<QString, QLineEdit*> jointPositionLineEdits;  // 用来存储关节名称和对应的 QLineEdit
    QMap<QString, QLineEdit*> jointVelocityLineEdits;  // 用来存储关节名称和对应的速度 QLineEdit


    void on_msgStatuReceived(const QString &msg);  // 订阅到消息时处理
    QList<DdsClientWrapper*> ddsClients_; // 保存所有创建的客户端
    void processStatuMessage(const QString &msg);//解析 JSON 数据
    void processEventMessage(const QString &jsonString);//解析 JSON 数据
    void subEventCallBack(const QString &jsonString);
    void nodeInit();
    void on_msgEventReceived(const QString &msg);
    void subTestCallback(const CnpMsg& msg);
    void UpdateUI();

     CNPClient* subscriberClient;

    std::vector<QString> jointNameList;

     std::once_flag onceFlag_; // 确保只执行一次
    QMutex mJointStatuMutex;




};

#endif // JOINTSTATUSWIDGET_H
